<?
/**
  * Objectbase PHP Class
  * v.000000001
  * By Harper Reed
  * Harper@nata2.org
  * I am pretty awesome
  * 
  * I hate documenting my code. I cannot lie. But its pretty easy. 
  * I will make more documentation later. Basically you just do this:
  * 
  *	include "class.objectbase.php";
  *	$objectBase = new objectBase(); // instantiate the class
  *	$datadir = "./data"; // set the cache dir
  *	$datahash = "example"; // set the md5 hash
  *	$cachetime = 360; // the cache time
  *	$objectBase->config($datadir, $datahash); // config the class (not required)
  *
  *	if ($objectBase->getAge("example")>$cachetime) //check if object is cached and if so how long ago
  *	{
  *		$exampleResults = doSomeStuffTHatNeedsCacheing($stuff); // get stuff/object
  *		$objectBase->setObject("example",$exampleResults); // store object
  *	}
  *	$exampleResults = $objectBase->getObject("example"); // get object
  *
  * This will successfully cache the $exampleResults object - no matter 
  * what the results. It will be in a hashed dir in the data dir. There 
  * should be no collisions with other cached data, however MD5s can collide. 
  *
  * -----------------------------------------------------------
  *
  * Copyright 2006 Harper Reed
  * 
  * -----------------------------------------------------------
  *
  * LICENSE
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License (GPL)
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * To read the license please visit http://www.gnu.org/copyleft/gpl.html
  *
  */
class objectBase{


function objectBase(){
	$this->dataDir = "./data/";
	$this->hash = "objectbase";
}

function config($_dataDir, $_hash ='objectbase'){
	$this->dataDir = $_dataDir;
	$this->hash = $_hash;
}

function setError($message){
	$this->errorStatus =1;
	$this->errorMessage = $message;
	return 0;
}

function writeFile($_name, $_data){
	$hashname = md5($this->hash.$_name);
	$objectdir = $this->dataDir.substr($hashname,0,5);
	if(!is_dir($objectdir)){
		 mkdir($objectdir);
	}
	
	$filename = $objectdir."/". $hashname;

	if (!$handle = fopen($filename, 'w')) {
        	return  $this->setError("Cannot open file ($filename)");
   	}

	if (fwrite($handle, $_data) === FALSE) {
		return  $this->setError("Cannot write to file ($filename)");
   	}
  
	fclose($handle);
}

function getObject($_name){
	$hashname = md5($this->hash.$_name);
	$objectdir = $this->dataDir.substr($hashname,0,5);
	$filename = $objectdir."/". $hashname;
	if (file_exists($filename)){
	$readcontent = file_get_contents($filename);
	$unserializedcontent = unserialize($readcontent);
	return $unserializedcontent;
	}return null;
}

function setObject($_name,$_data){
	$data = serialize($_data);
	return $this->writefile($_name,$data);
}

function errorHandler(){
	return $this->errorMessage;
}

function getAge($_name){
	$hashname = md5($this->hash.$_name);
        $objectdir = $this->dataDir.substr($hashname,0,5);
        $filename = $objectdir."/". $hashname;
	if (file_exists($filename)){
		$mtime = filemtime($filename);
		$age = mktime() - $mtime;
	}else $age = 100000;
	return $age;
}

}

?>
